package csu.web.mypetstore.service;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;

import java.util.Objects;

public class AccountService {

    private AccountDao accountDao;
    public AccountService(){
        this.accountDao = new AccountDaoImpl();
    }
    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountDao.getAccountByUsernameAndPassword(account);
    }

    public Account getAccount(String username) {
        return accountDao.getAccountByUsername(username);
    }

    public void insertAccount(Account account) { //插入一个新的账户信息及其相关信息
        accountDao.insertAccount(account);
        accountDao.insertProfile(account);
        accountDao.insertSignon(account);
        accountDao.insertAccountCart(account);
        accountDao.insertAccountLog(account);
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
        accountDao.updateProfile(account);
        if (Objects.equals(account.getFavouriteCategoryId(), "BIRDS"))
            account.setBannerName("<image src=\"img/banner_birds.gif\">");
        else if (Objects.equals(account.getFavouriteCategoryId(), "CATS"))
            account.setBannerName("<image src=\"img/banner_cats.gif\">");
        else if (Objects.equals(account.getFavouriteCategoryId(), "DOGS"))
            account.setBannerName("<image src=\"img/banner_dogs.gif\">");
        else if (Objects.equals(account.getFavouriteCategoryId(), "FISH"))
            account.setBannerName("<image src=\"img/banner_fish.gif\">");
        else if (Objects.equals(account.getFavouriteCategoryId(), "REPTILES"))
            account.setBannerName("<image src=\"img/banner_reptiles.gif\">");
        if (account.getPassword() != null && account.getPassword().length() > 0) {
            accountDao.updateSignon(account);
        }
    }
}
